﻿using System;
using System.Diagnostics;
using UnityEngine.Assertions;

namespace Runtime.Services.Battle {

    /// <summary>
    /// 战斗单元格索引
    /// </summary>
    [DebuggerStepThrough]
    [DebuggerNonUserCode]
    public readonly struct DFR_BattleCellIndex : IComparable<DFR_BattleCellIndex> {

        public static readonly DFR_BattleCellIndex Invalid = new();

        private readonly bool m_IsValid;
        private readonly int m_Value;

        /// <summary>
        /// 值
        /// </summary>
        public int Value {
            get => m_Value;
            init {
                m_IsValid = true;
                m_Value = value;
            }
        }

        public DFR_BattleCellAxis ToAxis() {
            Assert.IsTrue(m_IsValid);
            return new() {
                Y = Value / (int)BattleServiceBase.Instance.HorizontalCellCount - (int)BattleServiceBase.Instance.VerticalCellCount / 2,
                X = Value % (int)BattleServiceBase.Instance.HorizontalCellCount - (int)BattleServiceBase.Instance.HorizontalCellCount / 2,
            };
        }

        public override bool Equals(object obj) {
            if (obj is DFR_BattleCellIndex other) {
                return other.m_IsValid == m_IsValid && other.Value == Value;
            }
            return false;
        }

        public override string ToString() {
            if (m_IsValid) {
                return $"[{nameof(DFR_BattleCellIndex)}({Value})]";
            } else {
                return $"[{nameof(DFR_BattleCellIndex)}({nameof(Invalid)})]";
            }
        }
        public override int GetHashCode() {
            Assert.IsTrue(m_IsValid);
            return Value.GetHashCode();
        }

        int IComparable<DFR_BattleCellIndex>.CompareTo(DFR_BattleCellIndex other) => Value.CompareTo(other.Value);

        public static bool operator ==(DFR_BattleCellIndex left, DFR_BattleCellIndex right) => left.Equals(right);
        public static bool operator !=(DFR_BattleCellIndex left, DFR_BattleCellIndex right) => !left.Equals(right);
    }
}
